const User = require('./user.model');
const Role = require('./role.model');
const Permission = require('./permission.model');
const Device = require('./device.model');
const Schedule = require('./schedule.model');
const Payment = require('./payment');
const Order = require('./order.model');
const SystemLog = require('./systemLog.model');
const Application = require('./application.model');
const MapService = require('./mapService.model');
const CampusAccount = require('./campusAccount.model');
const PointRule = require('./pointRule.model');
const PointTransaction = require('./pointTransaction.model');
const MemberLevel = require('./memberLevel.model');
const MiniProgram = require('./miniProgram.model');
const OrderItem = require('./orderItem.model');
const Product = require('./product.model');
const Refund = require('./refund');

// User - Role relationships
User.belongsToMany(Role, { 
  through: 'UserRoles',
  indexes: [
    {
      unique: true,
      fields: ['UserId', 'RoleId'],
      name: 'user_role_unique'
    }
  ]
});
Role.belongsToMany(User, { 
  through: 'UserRoles'
});

// Role - Permission relationships
Role.belongsToMany(Permission, { 
  through: 'RolePermissions',
  indexes: [
    {
      unique: true,
      fields: ['RoleId', 'PermissionId'],
      name: 'role_permission_unique'
    }
  ]
});
Permission.belongsToMany(Role, { 
  through: 'RolePermissions'
});

// User - Device relationships
User.hasMany(Device);
Device.belongsTo(User);

// User - Schedule relationships
User.hasMany(Schedule);
Schedule.belongsTo(User);

// Device - Schedule relationships
Device.hasMany(Schedule);
Schedule.belongsTo(Device);

// User - Order relationships
User.hasMany(Order);
Order.belongsTo(User);

// Order - Payment relationships
Order.hasMany(Payment);
Payment.belongsTo(Order);

// User - SystemLog relationships
User.hasMany(SystemLog);
SystemLog.belongsTo(User);

// User - CampusAccount relationships
User.hasOne(CampusAccount);
CampusAccount.belongsTo(User);

// User - PointTransaction relationships
User.hasMany(PointTransaction);
PointTransaction.belongsTo(User);

// PointRule - PointTransaction relationships
PointRule.hasMany(PointTransaction);
PointTransaction.belongsTo(PointRule);

// User - MemberLevel relationships
User.belongsTo(MemberLevel);
MemberLevel.hasMany(User);

// Application - User relationships (for app management)
Application.belongsToMany(User, { 
  through: 'UserApplications',
  indexes: [
    {
      unique: true,
      fields: ['ApplicationId', 'UserId'],
      name: 'user_application_unique'
    }
  ]
});
User.belongsToMany(Application, { 
  through: 'UserApplications'
});

// MiniProgram - User relationships
MiniProgram.belongsToMany(User, { 
  through: 'UserMiniPrograms',
  indexes: [
    {
      unique: true,
      fields: ['MiniProgramId', 'UserId'],
      name: 'user_miniprogram_unique'
    }
  ]
});
User.belongsToMany(MiniProgram, { 
  through: 'UserMiniPrograms'
});

// MapService - Application relationships
MapService.belongsToMany(Application, { 
  through: 'ApplicationMapServices',
  indexes: [
    {
      unique: true,
      fields: ['MapServiceId', 'ApplicationId'],
      name: 'application_mapservice_unique'
    }
  ]
});
Application.belongsToMany(MapService, { 
  through: 'ApplicationMapServices'
});

// Order - OrderItem relationships
Order.hasMany(OrderItem, {
  foreignKey: 'order_id',
  as: 'order_items'
});
OrderItem.belongsTo(Order, {
  foreignKey: 'order_id'
});

// OrderItem - Product relationships
OrderItem.belongsTo(Product, {
  foreignKey: 'product_id'
});
Product.hasMany(OrderItem, {
  foreignKey: 'product_id'
});

// Payment - Refund relationships
Payment.belongsTo(Order, {
  foreignKey: 'order_id',
  as: 'order'
});

Order.hasMany(Payment, {
  foreignKey: 'order_id',
  as: 'payments'
});

Refund.belongsTo(Payment, {
  foreignKey: 'payment_id',
  as: 'payment'
});

Payment.hasMany(Refund, {
  foreignKey: 'payment_id',
  as: 'refunds'
});

module.exports = {
  User,
  Role,
  Permission,
  Device,
  Schedule,
  Payment,
  Order,
  SystemLog,
  Application,
  MapService,
  CampusAccount,
  PointRule,
  PointTransaction,
  MemberLevel,
  MiniProgram,
  OrderItem,
  Product,
  Refund
}; 